package de.dhbw.karlsruhe.picsim.model.memory.impl;

import de.dhbw.karlsruhe.picsim.model.memory.HiddenRegisters;
import de.dhbw.karlsruhe.picsim.util.Bits;
import de.dhbw.karlsruhe.picsim.util.impl.BitUtilities;
import de.dhbw.karlsruhe.picsim.util.impl.Lengths;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jetbrains.annotations.NotNull;

import static de.dhbw.karlsruhe.picsim.util.impl.BitUtilities.fromLeastSignificantBitFirst;

/**
 * Created by IntelliJ IDEA.
 * User: goesen
 * Date: Apr 18, 2010
 * Time: 10:55:19 AM
 */
public final class HiddenRegistersImpl implements HiddenRegisters {
    private static final Log log = LogFactory.getLog(HiddenRegistersImpl.class);
    private Bits pcHighByte = BitUtilities.asLeastSignificantBitFirst(0, Lengths.PC_HIGH_BITS_LENGTHS);
    private Bits portALatch = BitUtilities.asLeastSignificantBitFirst(0, 5);
    private Bits portBLatch = BitUtilities.asLeastSignificantBitFirst(0, 8);

    @Override
    public Bits getPcHighBits() {
        return BitUtilities.clone(pcHighByte);
    }


    @Override
    public void setPcHighBits(@NotNull final Bits bits) {
        if (bits.getLength() != Lengths.PC_HIGH_BITS_LENGTHS) {
            throw new IllegalArgumentException(bits.toString());
        }
        log.debug("overwritting old pcHigh Bits " + fromLeastSignificantBitFirst(pcHighByte) +
                " with " + fromLeastSignificantBitFirst(bits));


        pcHighByte = BitUtilities.clone(bits);
    }

    @Override
    public Bits getPortALatch() {
        return BitUtilities.clone(portALatch);
    }

    @Override
    public void setPortALatch(final Bits portALatch) {
        this.portALatch = BitUtilities.clone(portALatch);

    }

    @Override
    public Bits getPortBLatch() {
        return BitUtilities.clone(portBLatch);
    }

    @Override
    public void setPortBLatch(final Bits portBLatch) {
        this.portBLatch = BitUtilities.clone(portBLatch);

    }
}
